package cn.gdcp.dialogbox;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn_dialog1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSimpleDialog();

            }
        });
        findViewById(R.id.btn_dialog2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSelectDialog();
            }
        });
        findViewById(R.id.btn_dialog3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSingleDialog();
            }
        });
        findViewById(R.id.btn_dialog4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMultipleDialog();
            }
        });
        findViewById(R.id.btn_dialog5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCustomDialog();
            }
        });
    }


    private void showSimpleDialog() {
        new AlertDialog.Builder(MainActivity.this)
        .setIcon(R.mipmap.ic_launcher)
        .setTitle("提示")
        .setMessage("你是不是今天生日")
        .setPositiveButton("是的", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int i) {
                Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_SHORT).show();
            }
        })
         .setNegativeButton("不是", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int i) {
                Toast.makeText(MainActivity.this,"NO",Toast.LENGTH_SHORT).show();
             }
         })
         .setNeutralButton("无所谓", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int i) {
                 Toast.makeText(MainActivity.this,"OK,it doesn't matter",Toast.LENGTH_SHORT).show();
             }
         })
        .create()
        .show();
    }

    private void showSelectDialog() {
        final String[] cities = {"北京","上海","广州","深圳"};
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("请选择你的城市")
                .setItems(cities, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        Toast.makeText(MainActivity.this,"你选择的城市是"+cities[i],Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
    }

    private void showSingleDialog() {
        final String[] sex = {"男","女","不知道"};
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("请选择你的性别")
                .setSingleChoiceItems(sex, -1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        Toast.makeText(MainActivity.this,"你的性别是"+sex[i],Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {

                    }
                })
                .create()
                .show();
    }

    private void showMultipleDialog() {
        final String[] hobbies = {"打代码","看书","学习","睡觉","吃东西"};
        final boolean isCheck[] = new boolean[6];
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("请选择你的爱好")
                .setMultiChoiceItems(hobbies, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i, boolean b) {
                        isCheck[i] = b;

                        if (b){
                            Toast.makeText(MainActivity.this,"你添加了"+hobbies[i],Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(MainActivity.this,"你取消了",Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                       StringBuffer buffer = new StringBuffer();
                       for (int j=0;j<isCheck.length;j++){
                           if (isCheck[j]){
                               buffer.append(hobbies[j]);
                           }
                       }
                       Toast.makeText(MainActivity.this,"你的爱好有"+buffer,Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();

    }

    private void showCustomDialog() {
        final EditText editText1 = new EditText(MainActivity.this);
        final EditText editText2 = new EditText(MainActivity.this);

        View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_input,null,false);
        EditText nameEdit = view.findViewById(R.id.edt_name);
        final EditText ageEdit = view.findViewById(R.id.edt_age);
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("请输入你的名字年龄")
                .setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        String name = editText1.getText().toString();
                        int age = Integer.parseInt(ageEdit.getText().toString());
                        Toast.makeText(MainActivity.this, "你的姓名是"+name+"年龄是"+age, Toast.LENGTH_SHORT).show();
                        }
                })
                .create()
                .show();

    }
}
